#include "fig_player_craft.h"
#include "fighter.h"
#include "gen_game_state.h"
#include "fig_constants.h"
#include "fig_vars.h"
#include "fig_enemy_manager.h"
#include "fig_bullet_manager.h"
#include "fig_enemybullet_master.h"
#include "fig_sfx_manager.h"

#include "ui/ui_progress.h"
#include "lang/lang.h"



FIGPlayerCraft::NuclearBomb::NuclearBomb() : 
										m_pUIBomb(NULL),
										m_pUIBombCount(NULL),
										m_pUIProgress(NULL),
										m_pCurrentSFX(NULL)

{
	m_pUIBombCount = GENGameState::GetGame()->GetWidget("ui_nuclear_count");
	m_pUIBomb = GENGameState::GetGame()->GetWidget("ui_nuclear");
	m_pUIProgress =  (ui::UIProgress*)GENGameState::GetGame()->GetWidget("ui_nuclear_progress");

	GENGameState::GetGame()->SetWidgetFunctor(
										"ui_nuclear",
										UI_DELEGATE(FIGPlayerCraft::NuclearBomb, 
										OnButtonBomb),ui::UIWidget::WE_LMB_CLICK
										);

	m_CoolDown.SetLimit(1.0f);
    
}

void	FIGPlayerCraft::NuclearBomb::Tick(
										float dt,
										const math::Vec2& Pos
										)
{
	if(m_pUIBombCount)
		m_pUIBombCount->Tick(dt);

	if(m_pCurrentSFX)
	{
		if(m_pCurrentSFX->IsAlive())
		{
			m_pCurrentSFX->FollowMe(Pos);
			FIGEnemyBulletMaster::Instance().DieAll();
		}
		else
			m_pCurrentSFX = NULL;
	}

	m_CoolDown.Tick(dt);
	
	if(m_pUIProgress)
	{
		if(m_CoolDown.IsReady())
		{
			m_pUIProgress->SetIsVisible(false);
		}
		else
		{
			int Count = FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_BOMB_COUNT);
			if(Count > 0 )
			{
				m_pUIProgress->SetProgress(1.0f -m_CoolDown.GetProgress());
				m_pUIProgress->SetIsVisible(true);
			}
		}
	}
	
}

void	FIGPlayerCraft::NuclearBomb::Reset()
{
	if(m_pCurrentSFX)
	{
		m_pCurrentSFX->ToDie();
		m_pCurrentSFX = NULL;		
	}

	m_CoolDown.ForceReady();
}

void	FIGPlayerCraft::NuclearBomb::ShowCount()
{
	int Count = FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_BOMB_COUNT);
	if(m_pUIBombCount)
	{
		if(Count > 0)
			m_pUIBombCount->SetTextA(" x" + lang::Lang::FormatNumber(Count));
		else
			m_pUIBombCount->SetTextA("");

	}

	if(m_pUIBomb)
	{
		if(Count > 0)
		{
			m_pUIBomb->SetIsActive(true);
			m_pUIBomb->SetColor(1.0f,1.0f,1.0f,1.0f);
		}
		else
		{
			m_pUIBomb->SetIsActive(false);
			m_pUIBomb->SetColor(0.5f,0.5f,0.5f,0.5f);
		}
	}
}


void DELEGATE_CALL		FIGPlayerCraft::NuclearBomb::OnButtonBomb(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	//if( GENGameState::GetGame()->GetLastMouseClickWidget(pWidget) != pWidget)
	//	return;

	if(!m_CoolDown.IsReady())
		return;

	
	int BombCount = FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_BOMB_COUNT);
	if(BombCount <= 0)
		return;

	BombCount--;
	FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_BOMB_COUNT,BombCount);

	    
	const float BOMB_DAMAGE = 100.0f;
	FIGEnemyManager::Instance().DoDamangeToAll(BOMB_DAMAGE);
	Fighter::Instance().GetLevel()->DoDamangeToAll(BOMB_DAMAGE);
    
	FIGEnemyBulletMaster::Instance().DieAll();
	m_pCurrentSFX = FIGSFXManager::Instance().Spawn(
												FIGSfx::PLAYER_BOMB,
												FIGPlayerCraft::Instance().GetPos(),
												2.0f,
												0,
												false,
												FIGSounds::EXPLOSION_VERYBIG
												);

	ShowCount();
	m_CoolDown.Reset();
}



